package com.iwhalecloud.bss.kite.cucc.service.util;

import com.iwhalecloud.bss.kite.common.util.KiteDateUtils;
import com.iwhalecloud.bss.kite.common.util.SeqUtils;
import com.iwhalecloud.bss.kite.common.util.SnowflakeUtils;
import com.ztesoft.bss.common.util.DateUtil;

import java.util.Date;

/**
 * 拼接流水号
 * @author wuguoyuan
 * @date 2019-07-02 09:47:51
 */
public class LocalSerialNumUtil {

  /**
   * 获取流水号，替代 2.0 的写法
   * select to_char(sysdate,'yyyymmddhh24miss')||lpad(seq_wx_sendcheckcode.nextval,9,'0') from dual
   * @return
   */
  public static String getSerialNum(String format, String seqName, int length, String s) {
    StringBuffer serialNum = new StringBuffer();
    // 拼接时间 yyyyMMddHHmmss
    String nowStr = DateUtil.formatDate(new Date(), format);
    serialNum.append(nowStr);
    
    // 拼接序列
    String seq = SeqUtils.getNext(seqName);
    
    // 长度超过 length
    if (seq.length() > length) {
      serialNum.append(seq.substring(0, length));
    }
    // 长度不够指定长度,前面补0
    else if (seq.length() < length) {
      int cha = length - seq.length();
      for (int i = 0; i < cha; i++) {
        serialNum.append(s);
      }
      serialNum.append(seq);
    }
    
    return serialNum.toString();
  }
  
  public static String getOrderSerialNo() {
	  	StringBuffer serialNum = new StringBuffer();
	    // 拼接时间 yyyyMMddHH24
	    String nowStr = KiteDateUtils.getDateAndHour();
	    serialNum.append(nowStr);
	    serialNum.append(SnowflakeUtils.generateId());
	    return serialNum.toString();
  }
}
